package com.leetcode.array;

/**
 * Given two sorted integer arrays nums1 and nums2,
 *  merge nums2 into nums1 as one sorted array.
   Note:
   You may assume that nums1 has enough space
    (size that is greater or equal to m + n) 
    to hold additional elements from nums2. 
    The number of elements initialized in nums1 
    and nums2 are m and n respectively.
   
 * @author Adiy
 *
 */
public class MergeSortedArray {

	public static void main(String[] args) {
//		int[] nums1=new int[1000];
//		int[] nums2=new int[10];
//		for(int i=0;i<10;i++){
//			nums1[i]=2*i;
//			nums2[i]=2*i+1;
//		}
//	    for(int i=0;i<10;i++){
//	    	System.out.print(nums1[i]+",");
//	    }
//	    System.out.println();
//	    for(int i=0;i<10;i++){
//	    	System.out.print(nums2[i]+",");
//	    }
//	    System.out.println();
		int nums1[]=
		{2,0};
		int nums2[]={1};
		merge(nums1,1,nums2,1);
	}

    public static void merge(int[] nums1, int m, int[] nums2, int n) {
    	int j=0;
        int len=m+n;
    	if(m==0&&n!=0){
        	for(int i=0;i<n;i++)
        	nums1[i]=nums2[i];
        }else if(m!=0&&n==0){
        	
        }else if(m==0&&n==0){
        	nums1=null;
        }else{
    
    	for(int i=0;i<len&&j<n;i++){
    		if(nums1[i]>nums2[j]){
    			int k=m;
    			while(k>i){
    				nums1[k]=nums1[k-1];
    				k--;
    			}
       			nums1[i]=nums2[j];
       			j++;m++;
    			i--;
    		}
    	}
    	for(int i=m;j<n;j++){
    		nums1[i]=nums2[j];
    		i++;
    	}
        }
    	for(int i=0;i<len;i++){
    		System.out.print(nums1[i]+",");
    	}
    }
}
